﻿#include "TurnTableCode_TurnTable_SetRateMode.h"

#include "TurnTableCodec.h"

TurnTableCode_TurnTable_SetRateMode::TurnTableCode_TurnTable_SetRateMode(TurnTableCodec *turnTableCodec)
{
  this->turnTableCodec = turnTableCodec;
}

TurnTableCode_TurnTable_SetRateMode::~TurnTableCode_TurnTable_SetRateMode()
{
}

QByteArray TurnTableCode_TurnTable_SetRateMode::code(float arg1, float arg2, float arg3)
{
  const float turnTableRateLowerLimit = turnTableCodec->getTurnTableRateLowerLimit();
  const float turnTableRateUpperLimit = turnTableCodec->getTurnTableRateUpperLimit();
  if (-turnTableRateUpperLimit > arg1) //如果速率参数小于负的上限，则返回
  {
    return QByteArray();
  }
  if (turnTableRateUpperLimit < arg1) //如果速率参数大于上限，则返回
  {
    return QByteArray();
  }
  if (-turnTableRateLowerLimit < arg1 && turnTableRateLowerLimit > arg1) //如果速率参数处于正负下限之间，则返回
  {
    return QByteArray();
  }
  if (turnTableCodec->getTurnTableAccelerationLowerLimit() > arg2) //如果加速度参数小于下限，则返回
  {
    return QByteArray();
  }
  if (turnTableCodec->getTurnTableAccelerationUpperLimit() < arg2) //如果加速度参数大于上限，则返回
  {
    return QByteArray();
  }
  //参数全部正确，则返回组装的指令
  return addSummnation(QByteArray().append((uchar)TurnTableCodec::TurnTable_DeviceNum)
                       .append(1,(uchar)TurnTableCodec::TurnTable_AxleNumOne)
                       .append((uchar)TurnTableCodec::TurnTable_SetRateMode)
                       .append(floatToByteArray(arg1))
                       .append(floatToByteArray(arg2))
                       .append(4,0));
}
